home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / mtlxms21 / xms.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  11.8 KB  |  263 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      XMS.H: header file for XMS/UMB/HMA class library.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //     MODIFIED BY MAXTAL P/L August 1994
  15. //    Modifier: John Skaller, maxtal@suphys.physics.su.oz.au
  16. //    Changes:
  17. //        added XMS::havail() - # available handles
  18. //        changed XMSptr to use constructor
  19. //        changed XMScopy to work for odd # of bytes
  20. //        added XMS::get/put members for one byte transfer
  21. //        changed XMSptr to use operator[]
  22. //
  23. //    Warning: contains Borland specific code
  24. //    Warning: odd byte transfers are not properly error checked
  25. //
  26. //    Recommendation: DONT use these classes. Use a 32 bit DOS
  27. //        extender or operating system instead.
  28. //        Real mode is obsolete. XMS is obsolete.
  29. //--------------------------------------------------------------------------
  30. //
  31. //      Revision history:
  32. //      2.0     Nov 1993        This file added to provide general XMS
  33. //                              facilities
  34. //    2.1    Aug 1994    Modifed by Maxtal
  35. //--------------------------------------------------------------------------
  36.  
  37. #ifndef __XMS_H
  38. #define __XMS_H
  39.  
  40. //--------------------------------------------------------------------------
  41. //
  42. //      Class XMS: wrapper class for XMS driver functions.
  43. //
  44. //      Declaring an object of this class allocates a new XMS handle.
  45. //      The constructor takes an optional long integer parameter giving
  46. //      the size of the XMS block to be allocated in bytes, which may be
  47. //      rounded up; if it is omitted, a block size of zero will be assumed.
  48. //      The following operations are available:
  49. //
  50. //      object.valid ()       -- Returns non-zero if the object has been
  51. //                               created successfully.
  52. //      !object               -- Returns the (non-zero) error code if the
  53. //                               object has NOT been created successfully.
  54. //      object.size ()        -- Returns the current block size in bytes.
  55. //      object.resize (n)     -- Resizes the block to "n" bytes.  Returns
  56. //                               a result of type XMS::error.
  57. //      object.at (n)         -- Returns a descriptor pointing to the
  58. //                               n-th byte of an XMS block (see below).
  59. //      object [n]            -- A synonym for "object.at (n)".
  60. //      XMS::copy (to,from,n) -- Copies "n" bytes from "from" to "to"; can
  61. //                               be used to copy from real memory to XMS or
  62. //                               vice versa or from XMS to XMS. For accessing
  63. //                               real memory, "to" or "from" can be a pointer
  64. //                               of any type; for accessing XMS, they must be
  65. //                               descriptors generated by "at" or the "[]"
  66. //                               operator (above).  Returns a result of type
  67. //                               XMS::error.
  68. //      XMS::available ()     -- Returns the total number of bytes of XMS
  69. //                               currently available.
  70. //      XMS::largest ()       -- Returns the size of the largest contiguous
  71. //                               unallocated XMS block in bytes.
  72. //      XMS::havail ()        -- get no of handles (Added by MAXTAL)
  73. //
  74. //      Note that copy operations should specify an even number of bytes,
  75. //      and that source and destination should not overlap if an XMS-to-XMS
  76. //      copy is made.  Performance may be improved if copies to or from real
  77. //      memory are made to word aligned addresses (doubleword aligned for
  78. //      32-bit processors).
  79. //
  80. //      BEWARE: If programs are terminated abnormally, any existing XMS
  81. //      blocks will remain unrecoverably allocated.
  82.  
  83. class XMS
  84. {
  85.     struct XMSptr //MAXTAL: redesigned!
  86.     {
  87.       int handle;
  88.       unsigned long offset;
  89.       XMSptr(int h=0, unsigned long o=0L) : handle(h), offset(o) {}
  90.       XMSptr operator[](unsigned long l)const
  91.       {
  92.         return XMSptr (handle, offset+l);
  93.       }
  94.     };
  95.  
  96.  
  97.   public:
  98.     enum error {                            // XMS error codes ...
  99.         SUCCESS                             = 0x00,
  100.         NO_DRIVER                           = 0x01,
  101.         INVALID_OBJECT                      = 0x02,
  102.         BLOCK_TOO_BIG                       = 0x03,
  103.         NOT_IMPLEMENTED                     = 0x80,
  104.         VDISK_DETECTED                      = 0x81,
  105.         A20_ERROR                           = 0x82,
  106.         GENERAL_DRIVER_ERROR                = 0x8E,
  107.         UNRECOVERABLE_DRIVER_ERROR          = 0x8F,
  108.         NO_HMA                              = 0x90,
  109.         HMA_IN_USE                          = 0x91,
  110.         HMA_REQUEST_TOO_SMALL               = 0x92,
  111.         NO_MORE_MEMORY                      = 0xA0,
  112.         NO_MORE_HANDLES                     = 0xA1,
  113.         BAD_HANDLE                          = 0xA2,
  114.         BAD_SRC_HANDLE                      = 0xA3,
  115.         BAD_SRC_OFFSET                      = 0xA4,
  116.         BAD_DST_HANDLE                      = 0xA5,
  117.         BAD_DST_OFFSET                      = 0xA6,
  118.         BAD_LENGTH                          = 0xA7,
  119.         OVERLAP_ERROR                       = 0xA8,
  120.         PARITY_ERROR                        = 0xA9,
  121.         BLOCK_LOCKED                        = 0xAB
  122.     };
  123.  
  124.     XMS    (long size = 0);                 // constructor
  125.     ~XMS   ();                              // destructor
  126.  
  127.     error  operator!  ()                    { return status; }
  128.     int    valid      ()                    { return (status == SUCCESS); }
  129.     long   size       ()                    { return allocation; }
  130.     error  resize     (long newsize);       // resize block
  131.  
  132.     XMSptr at         (long offset);        // offset into allocated block
  133.     XMSptr operator[] (long offset)         { return at (offset); }
  134.  
  135.     static error copy (const XMSptr& to,    // copy XMS -> XMS
  136.                        const XMSptr& from,  // (NB: to/from mustn't overlap)
  137.                        unsigned      len);  // length rounded down if odd
  138.  
  139.     static error copy (void far*     to,    // copy XMS -> conventional
  140.                        const XMSptr& from,
  141.                        unsigned      len);  // length rounded down if odd
  142.  
  143.     static error copy (const XMSptr& to,    // copy conventional -> XMS
  144.                        void far*     from,
  145.                        unsigned      len);  // length rounded down if odd
  146.  
  147.     static unsigned char
  148.                   get (const XMSptr&);      //MAXTAL: one byte fetch
  149.  
  150.     static void put   (const XMSptr&,
  151.                        unsigned char);       //MAXTAL: on byte store
  152.  
  153.     static long       available ();         // get total size of free XMS
  154.     static long       largest   ();         // get size of largest free block
  155.     static unsigned long  havail    ();     //MAXTAL: get no of handles
  156.  
  157.   private:
  158.     error status;
  159.     int   handle;
  160.     long  allocation;
  161. };
  162.  
  163. //--------------------------------------------------------------------------
  164. //
  165. //      Class UMB: wrapper class for XMS driver UMB functions.
  166. //
  167. //      Declaring an object of this class attempts to allocate an upper
  168. //      memory block (UMB) of the specified size.  The constructor takes
  169. //      a parameter specifying the block size in bytes, which may be
  170. //      rounded up.  The following operations are available:
  171. //
  172. //      UMB::largest ()     -- Returns the size of the largest available
  173. //                             UMB block in bytes.
  174. //      object.addr ()      -- Returns a pointer to the allocated UMB, or
  175. //                             a null pointer if the allocation failed.
  176. //      object.valid ()     -- Returns non-zero if the object has been
  177. //                             created successfully.
  178. //      !object             -- Returns the (non-zero) XMS error code if the
  179. //                             object has NOT been created successfully.
  180. //      object.size ()      -- Returns the actual allocated UMB size in bytes.
  181. //
  182. class UMB
  183. {
  184.   public:
  185.     UMB  (long size);                   // constructor
  186.     ~UMB ();                            // destructor
  187.  
  188.     static long largest   ();           // size of largest available block
  189.  
  190.     void huge*  addr      ()            { return address; }
  191.     XMS::error  operator! ()            { return status; }
  192.     int         valid     ()            { return (status == XMS::SUCCESS); }
  193.     long        size      ()            { return allocation; }
  194.  
  195.   private:
  196.     XMS::error status;
  197.     void huge* address;
  198.     long       allocation;
  199. };
  200.  
  201. //--------------------------------------------------------------------------
  202. //
  203. //      Class HMA: wrapper class for XMS driver HMA functions.
  204. //
  205. //      Declaring an object of this class attempts to allocate the HMA.
  206. //      The constructor takes an optional long integer parameter which
  207. //      gives the size of the HMA block to be allocated in bytes; if it
  208. //      is omitted, the maximum possible block size (0xFFF0 bytes) will
  209. //      be assumed.  The following operations are available:
  210. //
  211. //      object.valid ()       -- Returns non-zero if the HMA has been
  212. //                               allocated successfully.
  213. //      !object               -- Returns the (non-zero) XMS error code if the
  214. //                               HMA has NOT been allocated successfully.
  215. //      object.size ()        -- Returns the allocated HMA size in bytes.
  216. //      object.at (n)         -- Returns a descriptor pointing to the
  217. //                               n-th byte of the HMA (see below).
  218. //      object [n]            -- A synonym for "object.at (n)".
  219. //      XMS::copy (to,from,n) -- Copies "n" bytes from "from" to "to"; can
  220. //                               be used to copy from real memory to HMA or
  221. //                               vice versa or from HMA to HMA. For accessing
  222. //                               real memory, "to" or "from" can be a pointer
  223. //                               of any type; for accessing HMA, they must be
  224. //                               descriptors generated by "at" or the "[]"
  225. //                               operator (above).  Returns the number of
  226. //                               bytes actually copied.  Note that the source
  227. //                               and destination areas for HMS-to-HMS copy
  228. //                               operations should not overlap.
  229. //
  230. class HMA
  231. {
  232.     struct HMAptr                            { HMA* hma; unsigned offset; };
  233.  
  234.   public:
  235.     HMA  (unsigned size = 0xFFF0);           // constructor
  236.     ~HMA ();                                 // destructor
  237.  
  238.     XMS::error operator! ()                  { return status; }
  239.     int        valid     ()                  { return (status == XMS::SUCCESS); }
  240.     unsigned   size      ()                  { return allocation; }
  241.  
  242.     HMAptr at         (unsigned offset);     // offset into allocated block
  243.     HMAptr operator[] (unsigned offset)      { return at (offset); }
  244.  
  245.     static unsigned copy (const HMAptr& to,  // copy HMA -> HMA
  246.                           const HMAptr& from,
  247.                           unsigned      len);
  248.  
  249.     static unsigned copy (void far*     to,  // copy HMA -> conventional
  250.                           const HMAptr& from,
  251.                           unsigned      len);
  252.  
  253.     static unsigned copy (const HMAptr& to,  // copy conventional -> HMA
  254.                           void far*     from,
  255.                           unsigned      len);
  256.  
  257.   private:
  258.     XMS::error status;
  259.     unsigned   allocation;
  260. };
  261.  
  262. #endif
  263.